package cn.nexd.map.location.collector.util;


import android.annotation.TargetApi;
import android.os.Build;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import cn.nexd.map.location.cache.DiskLruCache;
import cn.nexd.map.location.collector.bean.WifiDataResult;
import cn.nexd.map.location.task.core.SimpleSafeTask;
import cn.nexd.map.location.utils.Log;

/**
 * WifiDataCacheThread create by codingfish at 15/4/24
 * TODO:
 *
 * @Version V1.0
 */
class PassiveModeDataUtil {
    private static String userId;
    private static String scanId;
    private static DiskLruCache diskLruCache;
    StringBuilder wifiStringBuilder;
    private int taskCount = 0;

    private PassiveModeDataUtil() {
        wifiStringBuilder = new StringBuilder();
    }

    public static PassiveModeDataUtil getInstance(String userId, String scanId, DiskLruCache diskLruCache) {
        PassiveModeDataUtil.userId = userId;
        PassiveModeDataUtil.scanId = scanId;
        PassiveModeDataUtil.diskLruCache = diskLruCache;
        return SingletonHolder.getInstance();
    }

    public void cacheWifiData(List<WifiDataResult> scanResults) {
        new CacheThread(userId, scanId, diskLruCache, scanResults).execute();
    }

    private static class SingletonHolder {
        private static PassiveModeDataUtil instance = new PassiveModeDataUtil();

        public static PassiveModeDataUtil getInstance() {
            return instance;
        }
    }

    private class CacheThread extends SimpleSafeTask {
        private String userId;
        private String scanId;
        private DiskLruCache diskLruCache;
        private List<WifiDataResult> scanResults;

        private CacheThread(String userId, String scanId, DiskLruCache diskLruCache, List<WifiDataResult> scanResults) {
            this.userId = userId;
            this.scanId = scanId;
            this.diskLruCache = diskLruCache;
            this.scanResults = scanResults;
        }

        @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
        @Override
        protected Object doInBackgroundSafely() throws Exception {

            for (int i = 0, len = scanResults.size(); i < len; i++) {
                WifiDataResult scanResult = scanResults.get(i);
                wifiStringBuilder.append(scanResult.getUserId()).append("#");
                wifiStringBuilder.append(scanResult.getId()).append("#");
                wifiStringBuilder.append(scanResult.getSsid()).append("#");
                wifiStringBuilder.append(scanResult.getBssid()).append("#");
                wifiStringBuilder.append(scanResult.getCapability()).append("#");
                wifiStringBuilder.append(scanResult.getLevel()).append("#");
                wifiStringBuilder.append(scanResult.getFrequency()).append("#");
                wifiStringBuilder.append(scanResult.getTimestamp()).append("#");
                wifiStringBuilder.append(scanResult.getSingleIntensity());// 强度计算
                wifiStringBuilder.append("\n");
            }

            if (taskCount == 10) {
                //每十次执行一次存储
                try {
                    DiskLruCache.Editor editor = diskLruCache.edit(scanId);
                    OutputStream outputStream = editor.newOutputStream(0);
                    outputStream.write(wifiStringBuilder.toString().getBytes());
                    editor.commit();
                    diskLruCache.flush();

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    System.gc();
                }
                wifiStringBuilder.delete(0, wifiStringBuilder.length());
                //taskCount 置为1，标识一次新的周期采集开始
                taskCount = 1;
                Log.d("cn.nexd.collection", "" + wifiStringBuilder.toString());
            }
            taskCount = taskCount + 1;
            return null;
        }
    }

}
